/*jslint sloppy: true */
/*global require, module */
var forEach = require('./util').forEach,
	extend = require('./util').extend,
	getUA = require('./util').getUA,
	CaseResult = require('./caseResult');

function Result(resultRaw, report) {
	var data = resultRaw.data;

	this.$$report = report;

	this.$caseResultList = [];
	this.$env = getUA(data.ua);

	this.$overview = {
		testingMachineTime: 0,
		allPassedNumber: 0,
		hasFailureNumber: 0,
		totalTimes: 0,
		passedFrequency: 0,
		failureFrequency: 0,
		envId: this.$getEnvId() //TODO准备废除
	};

	forEach(data.taskResult, function (caseResultOpts) {
		var nCR = new CaseResult(caseResultOpts, this),
			cOv = nCR.getOverview(),
			tOv = this.$overview;

		this.$caseResultList.push(nCR);
		tOv.testingMachineTime += cOv.testingMachineTime;
		tOv.passedFrequency += cOv.passedFrequency;
		tOv.failureFrequency += cOv.failureFrequency;
		tOv.totalTimes += cOv.times;
		tOv[cOv.isAllPassed ? 'allPassedNumber' : 'hasFailureNumber'] += 1;
	}, this);
}
Result.prototype.$getEnvId = function () {
	//TODO 准备废除
	var e = this.$env;
	return e.os + '-' + e.browser;
};
Result.prototype.getOverview = function () {
	return extend({}, this.$overview);
};
Result.prototype.getEvn = function () {
	return this.$env;
};
Result.prototype.getUserAgent = function () {
	return this.$userAgentRaw;
};
Result.prototype.getPerformanceOverview = function () {
	var list = [];
	forEach(this.$caseResultList, function (cr) {
		var cPOL = cr.getPerformanceOverview();
		forEach(cPOL, function (cPO) {
			cPO.tR = this;
			list.push(cPO);
		}, this);
	}, this);
	return list;
};
Result.prototype.getCaseResultList = function () {
	var crList = [];
	forEach(this.$caseResultList, function (cr) {
		crList.push(extend({}, cr.$overview, {cr: cr}));
	});
	return crList;
};

module.exports = Result;
